#include <bits/stdc++.h>
#define rep(i, l, r) for(int i = l; i <= r; i++)
#define int long long
using namespace std;
const int MN = 1e5 + 5;
//Ryo The Rock!!!
int Choke();
signed main() {
    freopen("color.in", "r", stdin);
    freopen("color.out", "w", stdout);
    int T(10);
    cin >> T;
    while(T--) Choke();
    return 0;
}
int Choke() {
    int n;
    cin >> n;
    vector<int> a(n + 1);
    rep(i, 1, n) cin >> a[i];
    if(n <= 15) {
        int res = 0;
        rep(i, 0, (1 << n) - 1) {
            int temp = 0, lst = -1;
            rep(d, 0, n - 1) {
                int bit_now = (i >> d) & 1;
                if(bit_now) {
                    temp += (lst == a[d + 1] ? lst : 0);
                    lst = a[d + 1];
                }
            }
            lst = -1;
            rep(d, 0, n - 1) {
                int bit_now = (i >> d) & 1;
                if(!bit_now) {
                    temp += (lst == a[d + 1] ? lst : 0);
                    lst = a[d + 1];
                }
            }
            res = max(res, temp);
        }
        cout << res << endl;
        return 0;
    }
    //we can find out that this is definitely a dp.
    vector<int> stk1, stk2;
    map<int, int> mp;
    rep(i, 1, n) mp[a[i]]++;
    int res = 0;
    rep(i, 1, n) {
        if(mp[a[i]] == 1) continue;
        if(!stk1.empty() && a[i] > stk1[stk1.size() - 1])
            stk2.push_back(stk1[stk1.size() - 1]),
            stk1[stk1.size() - 1] = a[i];
        else if(!stk1.empty() && a[i] == stk1[stk1.size() - 1]) {
            res += a[i];
            stk2.push_back(stk1[stk1.size() - 1]);
            stk1.pop_back();
        }
        else if(stk1.empty()) stk1.push_back(a[i]);
        else stk2.push_back(a[i]);
    }
    for(int i = 1; i < stk2.size(); i++) res += (stk2[i] == stk2[i - 1]) * stk2[i];
    cout << res << endl; 
    return 0;
}